package com.hwx.mq.shell;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.nio.charset.StandardCharsets;

public class BaseProducer {
    public static void main(String[] args) throws MQClientException, MQBrokerException, RemotingException, InterruptedException {
        // 1. 指定生产者组
        DefaultMQProducer producer = new DefaultMQProducer("producer_group");
        // 2. 指定nameservice地址
        producer.setNamesrvAddr("127.0.0.1:9876");
        // 3. 启动生产者
        producer.start();
        // 4. 发送消息
        for (int i = 0; i < 500; i++) {
            Message message = new Message("user", "student", ("hello rocketmq " + i).getBytes(StandardCharsets.UTF_8));
            SendResult sendResult = producer.send(message);
            System.out.println(sendResult);
        }
        // 5. 关闭生产者
        producer.shutdown();
    }
}
